package com.sinbad.produce.mqconfig;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * fanout交换器模式（一对多）：公众号推送、邮件群发、广告
 */

@Configuration
public class FanoutConfigMQ {

    /***********************创建队列****************/
    @Bean(name = "queueA")
    public Queue queueA() {
        return new Queue("fanoutQueue.A", true, false, false);
    }

    @Bean(name = "queueB")
    public Queue queueB() {
        return new Queue("fanoutQueue.B", true, false, false);
    }

    @Bean(name = "queueC")
    public Queue queueC() {
        return new Queue("fanoutQueue.C", true, false, false);
    }


    /***********************创建一个基于Fanout的交换机****************/
    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange() {
        //配置广播路由器
        return new FanoutExchange("fanoutExchange", true, false);
    }


    /***********************队列和交换器绑定****************/

    /**
     * 三个Queue和一个fanout交换机绑定，实现广播模式
     * QueueA，QueueB，QueueC三个队列可以交给三个不同服务去监听，从而实现异步效果
     */
    @Bean
    public Binding bindingExchangeA(@Qualifier("queueA") Queue FanoutMessageA,
                                    @Qualifier("fanoutExchange") FanoutExchange fanoutexchange) {
        return BindingBuilder.bind(FanoutMessageA).to(fanoutexchange);
    }

    @Bean
    public Binding bindingExchangeB(@Qualifier("queueB") Queue FanoutMessageB,
                                    @Qualifier("fanoutExchange") FanoutExchange fanoutexchange) {
        return BindingBuilder.bind(FanoutMessageB).to(fanoutexchange);
    }

    @Bean
    public Binding bindingExchangeC(@Qualifier("queueC") Queue FanoutMessageC,
                                    @Qualifier("fanoutExchange") FanoutExchange fanoutexchange) {
        return BindingBuilder.bind(FanoutMessageC).to(fanoutexchange);
    }
}
